import java.awt.Point;
import com.speljohan.rsbot.script.Calculations;
import com.speljohan.rsbot.script.Random;
import com.speljohan.rsbot.script.wrappers.RSInterface;
import com.speljohan.rsbot.script.wrappers.RSNPC;
import com.speljohan.rsbot.script.wrappers.RSObject;
import com.speljohan.rsbot.script.wrappers.RSTile;

public class Prison extends Random {
	int PRISON_MATE = 3118;
	int LEVER_ID = 10817;
	int DOOR_KEY = 6966;
	int TELE_ANIM = 8939;
	int unlocked = 0;
	int state = 0;
	RSNPC balloonToPop = null;

	@Override
	public boolean activateCondition() {
		return (isLoggedIn() && findObject(LEVER_ID) != null);
	}

	@Override
	public String getAuthor() {
		return "Velocity";
	}

	@Override
	public String getName() {
		return "Prison Pete";
	}

	@Override
	public double getVersion() {
		return 2.0;
	}

	@Override
	public int loop() {
		switch (state) {
		case 0: // Find out what balloon to get.
			if (getMyPlayer().isMoving()) {
				return random(250, 500);
			}
			if (unlocked == 3) {
				state = 3;
			}
			if(getCameraAngle() < 175 || getCameraAngle() > 185) {
				setCameraRotation(random(175, 185));
				return random(500,750);
			}
			
			RSObject leverLoc = findObject(LEVER_ID);
			if (distanceTo(leverLoc.getLocation()) > 4) {
				walkTileMM(leverLoc.getLocation());
				return random(350, 500);
			} else {
				if (!getMyPlayer().isMoving() && atLever()) {
					if (balloonToPop != null) {
						state++;
					}
				}
			}
			return random(350, 400);
		case 1: // Find Animal and Pop it
			if (getMyPlayer().isMoving()) {
				return random(250, 500);
			}
			if (balloonToPop == null) {
				state = 0;
			}
			if (unlocked == 3) {
				state = 3;
			}
			if (!inventoryContains(DOOR_KEY)) {
				if (tileOnScreen(balloonToPop.getLocation())) {
					if (distanceTo(balloonToPop.getLocation()) < 5) {
						atNPC(balloonToPop, "Pop");
						return random(750, 900);
					} else if (!getMyPlayer().isMoving()) {
						walkTileMM(randomizeTile(balloonToPop.getLocation(), 2,2));
						return random(500,750);
					}
				} else if (!getMyPlayer().isMoving()) {
					walkTileMM(randomizeTile(balloonToPop.getLocation(), 2,2));
					return random(500,750);
				}
			}
			if (inventoryContains(DOOR_KEY)) {
				state++;
				return 1;
			}
			return random(350, 400);
		case 2: // Go to pete
			if (getMyPlayer().isMoving()) {
				return random(250, 500);
			}
			if (RSInterface.getInterface(241).isValid()) {
				atInterface(RSInterface.getInterface(241).getChild(5));
	            return random(350, 400);
			}
			if (inventoryContains(DOOR_KEY) && !getMyPlayer().isMoving()) {
				atInventoryItem(DOOR_KEY, "Return");
				balloonToPop = null;
				return random(1000, 2000);
			}
			if (!inventoryContains(DOOR_KEY) && getNearestNPCByID(PRISON_MATE) != null && unlocked <= 2) {
				unlocked++;
				state = 0;
				return random(350, 400);
			} else if (unlocked == 3) {
				state++;
				for(int w = 0; w < 4; w++) {
					if(canContinue())
						clickContinue();
					wait(random(750,1000));
				}
				return random(350, 400);
			}
			return random(350, 400);
		case 3: // Open door and leave
			RSObject templever = findObject(LEVER_ID);
			RSTile DoorLoc = new RSTile(templever.getLocation().getX() + 2, templever.getLocation().getY());
			if (unlocked == 3) {
				unlocked = 0;
				if(distanceTo(DoorLoc) > 8) {
					walkTileMM(randomizeTile(DoorLoc, 2, 0));
					wait(random(500,750));
					while(getMyPlayer().isMoving()) wait(random(150,300));
				}
				return random(1000, 2000);
			} else {
				if(getCameraAngle() < 175 || getCameraAngle() > 185) {
					setCameraRotation(random(175, 185));
					return random(500,750);
				}
				if (getMyPlayer().isMoving()) {
					return random(250, 500);
				}
				if (findObject(LEVER_ID) != null) {
					if(atTile(DoorLoc, 63, 1, 0)) {
						if(waitForAnim(random(1500,2000)) == TELE_ANIM) {
							wait(random(1500,2000));
							if(canContinue())
								clickContinue();
							wait(random(750,1000));
							if (getNearestNPCByID(PRISON_MATE) == null) {
								state = 0;
								return -1;
							}
						}
					} else {
						return random(500,750);
					}
				} else if (getNearestNPCByID(PRISON_MATE) != null) {
					if(canContinue())
						clickContinue();
					wait(random(750,1000));
					if (getNearestNPCByID(PRISON_MATE) == null) {
						state = 0;
						return -1;
					}
				}
			}
		}
		return -1;
	}

	public boolean atLever() {
		final RSObject leverObj = findObject(LEVER_ID);
		if (atTile(leverObj.getLocation(), 63, 0.4, 0)) {
			wait(random(750,1000));
			if(RSInterface.getInterface(273).getChild(3).isValid()) {
				balloonToPop = getNearestNPCByID(setItemIDs(RSInterface.getInterface(273).getChild(3).getModelID()));
				if (balloonToPop != null) {
					return true;
				}
			}
		}
		return false;
	}

	public int setItemIDs(final int b2p) {
		switch (b2p) {
		case 10749:
			return 3119;
		case 10750:
			return 3120;
		case 10751:
			return 3121;
		case 10752:
			return 3122;
		}
		return 0;
	}
	
	public boolean atTile(RSTile tile, int h, double xd, double yd) {
        try {
            Point location = Calculations.tileToScreen(tile.getX(), tile.getY(), xd, yd, h);
            if (location.x == -1 || location.y == -1) return false;
            clickMouse(location, 3, 3, true);
            wait(random(500, 1000));
            while (true) {
                if (!getMyPlayer().isMoving()) break;
                wait(random(500, 1000));
            }
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}